package com.jckj.core.service.impl;

import com.jckj.core.dao.ArticalAttachmentMapper;
import com.jckj.core.entity.ArticalAttachmentExample;
import com.jckj.core.entity.ArticalAttachmentKey;
import com.jckj.core.exception.ServiceException;
import com.jckj.core.service.ArticalAttachmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *     文章附件实现类
 * </p>
 * @author :liukuan
 * @date:2018/1/27.
 */
@Service
@Transactional(readOnly = true,rollbackFor = ServiceException.class)
public class ArticalAttachmentServiceImpl implements ArticalAttachmentService {

    private static Logger LOGGER = LoggerFactory.getLogger(ArticalAttachmentServiceImpl.class);

    @Autowired
    private ArticalAttachmentMapper articalAttachmentMapper;

    @Override
    @Transactional(readOnly = false,rollbackFor = ServiceException.class)
    public int insert(ArticalAttachmentKey articalAttachmentKey) {
        int num=0;
        try {
            num=articalAttachmentMapper.insert(articalAttachmentKey);
        } catch (Exception e) {
            LOGGER.error("保存文章附件信息：%s",e);
            throw new ServiceException("保存文章附件出错",e);
        }
        return num;
    }

    @Override
    @Transactional(readOnly = false,rollbackFor = ServiceException.class)
    public int update(String articalId,String attachmentId) {
        int num=0;
        try {
            ArticalAttachmentKey record=new ArticalAttachmentKey();
            record.setAttachementId(attachmentId);
            ArticalAttachmentExample example=new ArticalAttachmentExample();
            example.createCriteria().andArticalIdEqualTo(articalId);
            num=articalAttachmentMapper.updateByExampleSelective(record,example);
        } catch (Exception e) {
            LOGGER.error("修改文章附件信息：%s",e);
           throw new ServiceException("修改文章附件出错",e);
        }
        return num;
    }

    @Override
    @Transactional(readOnly = false,rollbackFor = ServiceException.class)
    public int delete(String articalId) {
        int num=0;
        try {
            ArticalAttachmentKey ak=new ArticalAttachmentKey();
            ak.setArticalId(articalId);
            num=articalAttachmentMapper.deleteByPrimaryKey(ak);
        } catch (Exception e) {
            LOGGER.error("删除文章附件信息：%s",e);
            throw new ServiceException("删除文章附件信息出错",e);
        }
           return num;
    }
}
